package com.rocksea.app.tasks;

import android.os.AsyncTask;

import com.rocksea.app.db.DbCenter;
import com.rocksea.app.db.history.HistoryTable;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.Map;
import java.util.TreeMap;

public class LoadHistoryTask extends AsyncTask<Void,Void,TreeMap<String,ArrayList<HistoryTable>>>
{

    private long startTime;
    private long endTime;
    private int mode;

    private LoadHistoryTask(long startTime, long endTime, QueryCallBack mQueryCallBack,int mode)
    {
        this.endTime = endTime;
        this.startTime = startTime;
        this.mQueryCallBack = mQueryCallBack;
        this.mode = mode;
    }

    @Override
    protected TreeMap<String, ArrayList<HistoryTable>> doInBackground(Void... voids)
    {
        if (mode == HistoryTable.TEST_ALL)
            return DbCenter.mHistoryDbHelper.queryGroupPlanNum(this.startTime,this.endTime);
        return DbCenter.mHistoryDbHelper.queryGroupPlanNum(this.startTime,this.endTime,mode);
    }

    @Override
    protected void onPostExecute(TreeMap<String, ArrayList<HistoryTable>> stringArrayListTreeMap)
    {
        if (mQueryCallBack != null)
            mQueryCallBack.onQueryCallBack(stringArrayListTreeMap);
    }

    public static void startLoadHistoryTask(long startTime, long endTime,QueryCallBack queryCallBack,int mode)
    {
        LoadHistoryTask loadHistoryTask = new LoadHistoryTask(startTime,endTime,queryCallBack,mode);
        loadHistoryTask.execute();
    }

    public static void startLoadHistoryTask(int daySize,QueryCallBack queryCallBack,int mode)
    {
        long startTime = System.currentTimeMillis();
        long endTime = startTime - DbCenter.DAY_OF_TIME * daySize;
        LoadHistoryTask loadHistoryTask = new LoadHistoryTask(startTime,endTime,queryCallBack,mode);
        loadHistoryTask.execute();
    }

    private QueryCallBack mQueryCallBack;

    public interface QueryCallBack
    {
        void onQueryCallBack(Map<String, ArrayList<HistoryTable>> tables);
    }


}

